#!/usr/bin/env python

"""\
Usage: %prog [options] <uri>

Take THREDDS XML and transform to equivalent RDF. Output to stdout (default)
or upload to URL.\
"""

from optparse import OptionParser
from sys import exit
import urllib2

from duetopia.thredds import transform

if __name__ == '__main__':
	op = OptionParser(usage=__doc__)
	op.add_option("-u", "--upload",
			help="Upload (HTTP POST) serialized RDF/XML to URL",
			action="store",
			default=False
	)
        op.add_option("-s", "--urlbase",
                        help="Specifiy base URL of THREDDS server",
                        action="store",
                        default=False
        )

	(options, args) = op.parse_args()
	if len(args) != 1:
		op.print_help()
		exit()
        urlbase = None
        if options.urlbase:
                urlbase = options.urlbase
	if options.upload:
		data = transform(args[0],urlbase=urlbase)
		request = urllib2.Request(options.upload, data)
		fp = urllib2.urlopen(request)
		response = fp.read()
		fp.close()
		if response:
			print response
	else:
		print transform(args[0], format=True,urlbase=urlbase)
